package net.trustie.model;

import java.util.Date;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

@ExtractBy("//*body[@id='nv_forum']/div[@id='wp']/div[@id='ct']/div[@id='postlist']")
public class linuxtone_Model implements AfterExtractor, ValidateExtractor {
	private String postUrl;	
	
	private Date crawledTime;
	
	private String extractTime;
	
	private int history;

	@ExtractBy("//*table[1]/tbody/tr/td[@class='plc']/h1[@class='ts']/a[@id='thread_subject']/text()")
	private String postTitle;
	
	@ExtractBy("//*div[1]/table/tbody/tr[1]/td[@class='plc']/div[@class='pct']/div[@class='pcb']/div/table/tbody/tr/td/allText()")
	private String postContent;
	
	@ExtractBy("//*table[1]/tbody/tr[1]/td[@class='pls']/div[@class='hm']/span[2]/text()")
	private int viewNum;
	
	@ExtractBy("//*table[1]/tbody/tr[1]/td[@class='pls']/div[@class='hm']/span[5]/text()")
	private int replyNum;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[1]/td[@class='plc']/div[@class='pi']/div[@class='pti']/div[@class='authi']/em/text()")
	private String postTime;
	
	@ExtractBy("//*div[1]/table/tbody/tr[1]/td[@class='pls']/div[@class='pi']/div[@class='authi']/a/text()")
	private String authorName;
	
	@ExtractBy("//*div[1]/table/tbody/tr[1]/td[@class='pls']/div[@class='pi']/div[@class='authi']/a/@href")
	private String authorUrl;
	
	@ExtractBy("//*table[1]/tbody/tr[1]/td[@class='plc']/h1[@class='ts']/a[1]/text()")
	private String postType;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='recommend_add']/i/span[@id='recommendv_add']/text()")
	private int voteUp;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='recommend_subtract']/i/span[@id='recommendv_subtract']/text()")
	private int voteDown;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='k_favorite']/i/span[@id='favoritenumber']/text()")
	private int favoritedNum;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='k_share']/i/span[@id='sharenumber']/text()")
	private int sharedNum;
	@Override
	public void afterProcess(Page page) {
		//处理帖子url	
		this.postUrl=page.getPageUrl();
		
		//处理抽取时间
		this.extractTime=DateHandler.getExtractTime();
		
		//处理爬取时间
		this.crawledTime=page.getTime();
		
		//处理发帖时间
		this.postTime=DateHandler.formatAllTypeDate(this.postTime, crawledTime);
		
		//处理作者url
		this.authorUrl="http://bbs.linuxtone.org/"+this.authorUrl;
	}

	@Override
	public void validate(Page page) {
		if(StringHandler.isAtLeastOneBlank(this.postTitle,this.postType)){
			page.setResultSkip(this, true);
			return;
		}
		
		if(!DateHandler.canFormatToDate(this.postTime)){
			page.setResultSkip(this, true);
			return;
		}	
	}

	public String getPostTitle() {
		return postTitle;
	}

	public String getPostContent() {
		return postContent;
	}

	public int getViewNum() {
		return viewNum;
	}

	public int getReplyNum() {
		return replyNum;
	}

	public String getPostTime() {
		return postTime;
	}

	public String getAuthorName() {
		return authorName;
	}

	public String getAuthorUrl() {
		return authorUrl;
	}

	public void setPostTitle(String postTitle) {
		this.postTitle = postTitle;
	}

	public void setPostContent(String postContent) {
		this.postContent = postContent;
	}

	public void setViewNum(int viewNum) {
		this.viewNum = viewNum;
	}

	public void setReplyNum(int replyNum) {
		this.replyNum = replyNum;
	}

	public void setPostTime(String postTime) {
		this.postTime = postTime;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public void setAuthorUrl(String authorUrl) {
		this.authorUrl = authorUrl;
	}

	public String getPostType() {
		return postType;
	}

	public void setPostType(String postType) {
		this.postType = postType;
	}

	public int getVoteUp() {
		return voteUp;
	}

	public int getVoteDown() {
		return voteDown;
	}

	public void setVoteUp(int voteUp) {
		this.voteUp = voteUp;
	}

	public void setVoteDown(int voteDown) {
		this.voteDown = voteDown;
	}

	public String getPostUrl() {
		return postUrl;
	}

	public void setPostUrl(String postUrl) {
		this.postUrl = postUrl;
	}
	
	public Date getCrawledTime() {
		return crawledTime;
	}

	public int getFavoritedNum() {
		return favoritedNum;
	}

	public int getSharedNum() {
		return sharedNum;
	}	

	public void setCrawledTime(Date crawledTime) {
		this.crawledTime = crawledTime;
	}

	public void setFavoritedNum(int favoritedNum) {
		this.favoritedNum = favoritedNum;
	}

	public void setSharedNum(int sharedNum) {
		this.sharedNum = sharedNum;
	}	

	public int getHistory() {
		return history;
	}

	public void setHistory(int history) {
		this.history = history;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}		
	
}
